/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.paroquiadegloria.ejc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.paroquiadegloria.ejc.entidade.Circulo;

/**
 *
 * @author jadson
 */
public class CirculoDao {

    public void inserirCirculo(Circulo circulo) throws Exception {
        StringBuilder sql = new StringBuilder();
        sql.append("insert into circulo (");
        sql.append("inej_cd_encontro, ");
        sql.append("circ_ds_titulo, ");
        sql.append("circ_ds_cor) ");
        sql.append("values(?,?,?)");

        Connection con = null;
        PreparedStatement ps = null;

        try {
            con = ConnectionFactory.getConnection();
            ps = con.prepareStatement(sql.toString());
            int index = 1;

            ps.setInt(index++, circulo.getCodigoEncontro().intValue());
            ps.setString(index++, circulo.getDescricao());
            ps.setString(index++, circulo.getCorHex());

            ps.execute();

        } catch (SQLException ex) {
            throw ex;
        } finally {
            if (ps != null) {
                ps.close();
            }
            if (con != null) {
                con.close();
            }
        }
    }

    /**
     * Retorna uma Lista com todos os circulos cadastrados em um encontro.
     *
     * @return
     * @throws Exception
     */
    public List<Circulo> obterCirculosPorEncontro(Integer codigoEncontro) throws Exception {
        String sql = "select * from circulo where inej_cd_encontro = ? order by circ_cd_circulo";
        Connection con = ConnectionFactory.getConnection();
        PreparedStatement ps = null;
        ResultSet resultSet = null;

        List<Circulo> circuloList = new ArrayList<Circulo>();
        try {
            ps = con.prepareStatement(sql);
            ps.setInt(1, codigoEncontro);

            resultSet = ps.executeQuery();

            while (resultSet.next()) {
                Circulo circulo = new Circulo();
                circulo.setCodigo(new Integer(resultSet.getInt("circ_cd_circulo")));
                circulo.setCodigoEncontro(new Integer(resultSet.getInt("inej_cd_encontro")));
                circulo.setCorHex(resultSet.getString("circ_ds_cor"));
                circulo.setDescricao(resultSet.getString("circ_ds_titulo"));

                circuloList.add(circulo);
            }

        } catch (SQLException ex) {
            throw ex;
        } finally {
            if (resultSet != null) {
                resultSet.close();
            }
            if (ps != null) {
                ps.close();
            }
        }

        return circuloList;
    }
}
